<template>
  <!-- 页面容器 -->
  <div class="app-container">
    <!-- 表单容器 -->
    <div class="common-container">
      <!-- 表单 -->
      <el-form :model="domainInsertForm" label-width="100px" style="padding: 20px">
        <!-- 指标域名称 -->
        <el-form-item label="指标域名称：">
          <el-input v-model="domainInsertForm.domainName" placeholder="请输入" clearable />
        </el-form-item>

        <!-- 数据源 -->
        <el-form-item label="数据源：">
          <el-select
            v-model="domainInsertForm.dataSourceId"
            placeholder="请选择"
            clearable
            @change="handleChange"
          >
            <el-option
              v-for="item in dataSourceOptions"
              :key="item.id"
              :label="item.aliasName || item.dataSourceName"
              :value="item.id!"
            />
          </el-select>
        </el-form-item>

        <!-- 描述 -->
        <el-form-item label="描述：">
          <el-input
            v-model="domainInsertForm.domainDescription"
            type="textarea"
            placeholder="请输入描述"
          />
        </el-form-item>

        <!-- 操作按钮 -->
        <el-form-item>
          <el-button @click="handleCancel">取消</el-button>
          <el-button type="primary" @click="handleConfirm">完成</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref, onMounted } from "vue";
import { ElMessage } from "element-plus";
import NewIndicatorFieldAPI, { dataSourceForm } from "@/api/dataSourceManage/createIndicatorField"; // 引入接口文件
import IndicatorManageAPI, { DataSourceList } from "@/api/strategy/strategy-index-database";
import IndicatorDomainAPI, { DomainGroupInsertParameter } from "@/api/indicationManage/domain";
// import { emit } from "process"; // 不需要导入这个
// import router from "@/router";

export default defineComponent({
  name: "IndicatorForm",
  props: {
    dataSourceId: {
      type: Number,
      required: true,
    },
  },
  emits: ["close", "success"],
  setup(props, context) {
    // 表单数据
    const form = reactive<dataSourceForm>({
      indicatorName: "", // 指标域名称
      dataSourceId: props.dataSourceId, // 数据源
      description: "", // 描述
    });

    // 初始化domainInsertForm并设置dataSourceId
    const domainInsertForm = reactive<DomainGroupInsertParameter>({
      dataSourceId: props.dataSourceId,
    });

    const dataSourceOptions = ref<DataSourceList[]>();

    async function getDataSourceList() {
      dataSourceOptions.value = await IndicatorManageAPI.DataSourceList();
    }

    function handleChange(id: number) {
      // 查找选中项的 label 并赋值给 dataSourceName
      const selectedItem = dataSourceOptions.value?.find((item) => item.id === id);
      if (selectedItem) {
        domainInsertForm.dataSourceName = selectedItem.dataSourceName;
      }
    }

    // 指标域选项
    const indicatorDomainOptions = [
      { label: "域名称1", value: "1" },
      { label: "域名称2", value: "2" },
    ];

    /**
     * 清空表单内容
     */
    const clearForm = () => {
      domainInsertForm.domainName = "";
      domainInsertForm.dataSourceId = 0;
      domainInsertForm.domainDescription = "";
    };

    /**
     * 取消操作：清空表单并提示用户
     */
    const handleCancel = () => {
      clearForm();
      ElMessage.info("已取消操作");
      context.emit("close");
    };

    /**
     * 完成操作：提交表单数据
     */
    const handleConfirm = async () => {
      // 表单校验
      if (!domainInsertForm.domainName || domainInsertForm.dataSourceId === null) {
        ElMessage.error("指标域名称和数据源不能为空！");
        // await indAPI.add(form);
        // ElMessage.success("提交成功！");
        // clearForm();
        // router.back(); // 提交成功后返回上一页
        // return;
      }

      try {
        // 调用接口提交表单数据
        // await indAPI.add(form);
        domainInsertForm.parentId = 0;
        domainInsertForm.domainType = 0;
        await IndicatorDomainAPI.GroupInsert(domainInsertForm);
        ElMessage.success("提交成功！");
        context.emit("success");
        clearForm(); // 提交成功后清空表单
      } catch (error) {
        // 错误处理
        ElMessage.error(`提交失败：${(error as Error).message}`);
      }
    };

    onMounted(() => {
      getDataSourceList();
    });

    return {
      domainInsertForm,
      form,
      indicatorDomainOptions,
      dataSourceOptions,
      handleCancel,
      handleConfirm,
      handleChange,
    };
  },
});
</script>

<style scoped>
.common-container {
  width: 100%;
  max-width: 600px;
  padding: 18px 0 0 10px;
  margin-bottom: 10px;
  background-color: var(--el-bg-color-overlay);
  border: 1px solid var(--el-border-color-light);
  border-radius: 4px;
  box-shadow: var(--el-box-shadow-light);
}
</style>
